<?php
/* include_once 'function.php'; */
/* include_once 'global.php'; */

class PaymentController extends Zend_Controller_Action
{
  private $db;
  private $content_table;
  private $pay_table;
  public function init()
  {
    Zend_Session::start();
    $session = new Zend_Session_Namespace('account');   
    if (!isset($session->user) || $session->user->get("id") < 1)
      {
	header("Location: /account/login");
	die();
      }
    $db_obj = new myDb();
    $this->db = $db_obj->getDb();
    $this->db->query('set names utf8; ');

    Zend_Db_Table::setDefaultAdapter($this->db);
    $this->content_table = new Application_Model_DbTable_Content();
    $this->pay_table = new Application_Model_DbTable_PayHistory();
  }

  public function indexAction()
  {
  }
  /* param
   * cont_id: content id which you want to buy
   */
  public function buyContentByIdAction()
  {
    Zend_Session::start();
    $session = new Zend_Session_Namespace('account');   
    if (!isset($session->user) || $session->user->get("id") < 1)
      {
	header("Location: /account/login");
	die();
      }

    $cont_id = $this->getRequest()->getParam('cont_id');
    /* $where = $this->db->quoteInto('id = ?', $cont_id); */
    /* $this->content_table->fetchRow($where); */
    $cont = new Application_Model_Content();
    $mapper = new Application_Model_ContentMapper;
    $mapper->find($cont_id, $cont);
    $this->view->course = $cont;
  }
  /* in:
   * buyer_id
   * cont_id
   * title
   * price
   * owner_id
   *
   * out: $res, the primary key of item which insert to pay_history 
   */
  public function submitBuyContentAction()
  {
    $form = $this->getRequest()->getParams();

    /* get content info */
    $cont = new Application_Model_Content();
    $mapper = new Application_Model_ContentMapper;
    $mapper->find($form['cont_id'], $cont);
    $this->view->course = $cont;

    /* check whether user already by this content */
    $pay_obj = new Application_Model_PayHistory();
    $pay_mapper = new Application_Model_PayHistoryMapper();
    $where = $this->db->quoteInto('buyer_id = ?', $form['buyer_id']). ' and '. 
      $this->db->quoteInto('cont_id = ?', $form['cont_id']). ' and ('.
	  $this->db->quoteInto('status = ?', Application_Model_PayHistory::$TRANSCATION_DOING) . ' or '.
	  $this->db->quoteInto('status = ?', Application_Model_PayHistory::$TRANSCATION_DONE).')';
    $pay_mapper->findObj($where, $pay_obj);
    if ($pay_obj->get($pay_obj->getPrimaryKey()))
      {
	/* this user has paied */
	$this->view->errno = ERROR_ALREADY_PAIED;
	$this->view->msg = "您已经购买了本课程，请直接观看。";
	header("Location: /video/play?cont_id=".$form["cont_id"]);
	return;
      }

    /* 查看余额是否足够 */
    Zend_Session::start();
    $session = new Zend_Session_Namespace('account');   
    if ($session->user->get("wealth") < $form['price'])
      {
	/* not enough wealth to pay */
	$this->view->errno = ERROR_NOT_ENOUGH_WEALTH;
	$this->view->msg = "余额不足，请充值后再来购买。";
	return ;
      }

    /*  从财富中减去金额 */
    $session->user->set("wealth", $session->user->get("wealth") - $form['price']);
    $user_mapper = new Application_Model_UserMapper();
    if ($user_mapper->save($session->user))
      {
	/* 添加订购记录 */
	$_POST['title'] = $cont->get("title");
	$pay_obj = new Application_Model_PayHistory($_POST);
	if ($pay_mapper->save($pay_obj))
	  {
	    $this->view->res = true;
	    return;
	  }
      }
    $this->view->res = false;
  }
  public function howToRefundAction()
  {

  }
}
